package com.itzyf.pokemondata.activity

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import com.avos.avoscloud.AVException
import com.avos.avoscloud.AVObject
import com.avos.avoscloud.AVQuery
import com.avos.avoscloud.FindCallback
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.itzyf.pokemondata.R
import com.itzyf.pokemondata.bean.CodeDo
import com.itzyf.pokemondata.entity.GiftCode
import com.itzyf.pokemondata.entity.HuntingGround
import com.itzyf.pokemondata.entity.PageVo
import com.itzyf.pokemondata.entity.ResultVo
import com.itzyf.pokemondata.service.PokemonService
import com.itzyf.pokemondata.utils.RetrofitUtils
import com.itzyf.pokemondata.utils.showErrToast
import com.tydic.baselibrary.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_code.*
import kotlinx.android.synthetic.main.activity_code.rv_list
import kotlinx.android.synthetic.main.activity_list.*
import retrofit2.Call
import retrofit2.Response
import java.util.*


/**
 * @author 依风听雨
 * @version 创建时间：2020/01/13 15:36
 */
class CodeActivity : BaseActivity() {
    var mDatas: MutableList<GiftCode?> = ArrayList()
    private lateinit var mAdapter: QuickAdapter

    override fun getContentViewId() = R.layout.activity_code

    override fun getToolbarTitle() = "礼包码"

    override fun handlerActivity() {
        mAdapter = QuickAdapter(R.layout.item_code, mDatas)
        rv_list.layoutManager = LinearLayoutManager(this)
        rv_list.adapter = mAdapter
        queryDatas()

        mAdapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.tvMuti -> {
                    copy(mDatas[position]?.muti)
                }
                R.id.tvIos -> {
                    copy(mDatas[position]?.ios)
                }
                R.id.tvApp -> {
                    copy(mDatas[position]?.application)
                }
                R.id.tvIos2 -> {
                    copy(mDatas[position]?.ios2)
                }
            }
        }
    }

    //系统剪贴板-复制:   s为内容
    private fun copy(s: String?) {
        val clipboard: ClipboardManager =
            getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        val clipData = ClipData.newPlainText(null, s)
        clipboard.primaryClip = clipData
        showSnackbar("复制成功：$s")
    }

    fun queryDatas() {
        showProgressDialog()
        RetrofitUtils.getInstance()
            .getApiService(PokemonService::class.java)
            .queryListGift(1)
            .enqueue(object : retrofit2.Callback<ResultVo<PageVo<GiftCode>>> {
                override fun onFailure(call: Call<ResultVo<PageVo<GiftCode>>>, t: Throwable) {
                    dismissProgressDialog()
                }

                override fun onResponse(
                    call: Call<ResultVo<PageVo<GiftCode>>>,
                    response: Response<ResultVo<PageVo<GiftCode>>>
                ) {
                    dismissProgressDialog()
                    val body = response.body()
                    if (body!!.code == 200) {
                        mDatas.addAll(body.result.records)
                        mAdapter.notifyDataSetChanged()
                    }
                }
            })
    }

    internal inner class QuickAdapter(layoutResId: Int, data: List<GiftCode?>) :
        BaseQuickAdapter<GiftCode, BaseViewHolder>(layoutResId, data) {

        override fun convert(helper: BaseViewHolder, item: GiftCode?) {
            helper.setText(R.id.tvName, item?.name)
            helper.setText(R.id.tvTimeRange, getString(R.string.code_time_range, item?.timeRange))
            helper.setText(R.id.tvContent, getString(R.string.code_content, item?.content))
            helper.setText(R.id.tvMuti, getString(R.string.code_muti, item?.muti))
            helper.setText(R.id.tvApp, getString(R.string.code_app, item?.application))
            helper.setText(R.id.tvIos, getString(R.string.code_ios, item?.ios))
            helper.setText(R.id.tvIos2, getString(R.string.code_ios2, item?.ios2))
            helper.addOnClickListener(R.id.tvMuti, R.id.tvApp, R.id.tvIos)
        }
    }


}